package Graphique;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Sash;
import org.eclipse.swt.widgets.Text;
import Command.*;
import Connexion.ChargeurdeDonnees;
import Connexion.ConnexionMysql;
import Connexion.mysqlConnector;
import Listener.ButtonListener;
import StaticValues.StaticTabs;
import Structure.Film;

/**
 * @author tangi
 * 
 */
public class CompositeCenter extends Composite {

	private Sash sash;
	private FormData compositeButtonData, compositeFilmData, sashData;
	private Button nouveau, enregistrer, suivant, precedent;
	private Composite compositeButton, compositeFilm;
	private RowLayout buttonLayout;
	private Label filmLabel, genreLabel, yearLabel, synopsisLabel, noteLabel;
	private Text filmText, synopsisArea;
	private GridLayout centerLayout;
	private GridData centerData, synopsisData;
	private Combo comboGenre, comboYear, comboNote;
	private ChargeurdeDonnees cDD;
	private CommandNew commandNew;
	private CommandSave commandSave;
	private MainFrame main;

	/**
	 * Constructor of the class
	 * 
	 * @param shell
	 * @param style
	 */
	public CompositeCenter(Composite shell, int style) {
		super(shell, style);
		initCommands();
		initComponent();
		cDD = new ChargeurdeDonnees();
	}

	private void initCommands() {
		commandNew = new CommandNew();
		commandNew.setCompositeCenter(this);

		commandSave = new CommandSave();
		commandSave.setCompositeCenter(this);
	}

	/**
	 * 
	 */
	private void initComponent() {
		initDataForm();
		initButtons();
		initFilmComposite();
	}

	private void initDataForm() {
		this.setLayout(new FormLayout());

		sash = new Sash(this, SWT.HORIZONTAL);

		sashData = new FormData();
		sashData.left = new FormAttachment(0, 0);
		sashData.top = new FormAttachment(80, 0);
		sashData.right = new FormAttachment(100, 0);

		compositeFilmData = new FormData();
		compositeFilmData.left = new FormAttachment(0, 0);
		compositeFilmData.right = new FormAttachment(100, 0);
		compositeFilmData.top = new FormAttachment(0, 0);
		compositeFilmData.bottom = new FormAttachment(sash, 0);

		compositeButtonData = new FormData();
		compositeButtonData.left = new FormAttachment(0, 0);
		compositeButtonData.right = new FormAttachment(100, 0);
		compositeButtonData.top = new FormAttachment(sash, 0);
		compositeButtonData.bottom = new FormAttachment(100, 0);

		sash.setLayoutData(sashData);

		compositeFilm = new Composite(this, SWT.NONE);
		compositeFilm.setLayoutData(compositeFilmData);

		compositeButton = new Composite(this, SWT.BORDER);
		compositeButton.setLayoutData(compositeButtonData);

	}

	private void initButtons() {
		buttonLayout = new RowLayout();
		buttonLayout.justify = true;

		compositeButton.setLayout(buttonLayout);
		nouveau = new Button(compositeButton, SWT.NONE);
		nouveau.setText("Nouveau");
		nouveau.addListener(SWT.Selection, new ButtonListener(commandNew));

		enregistrer = new Button(compositeButton, SWT.NONE);
		enregistrer.setText("Enregistrer");

		precedent = new Button(compositeButton, SWT.NONE);
		precedent.setText("Précédent");

		suivant = new Button(compositeButton, SWT.NONE);
		suivant.setText("Suivant");

	}

	private void initFilmComposite() {
		centerLayout = new GridLayout(4, false);
		centerLayout.horizontalSpacing = 30;
		centerLayout.verticalSpacing = 10;

		compositeFilm.setLayout(centerLayout);

		centerData = new GridData();
		centerData.minimumWidth = 180;
		centerData.grabExcessHorizontalSpace = true;

		synopsisData = new GridData();
		synopsisData.minimumWidth = 600;
		synopsisData.minimumHeight = 300;
		synopsisData.horizontalSpan = 5;
		synopsisData.grabExcessHorizontalSpace = true;
		synopsisData.grabExcessVerticalSpace = true;

		filmLabel = new Label(compositeFilm, SWT.NONE);
		filmLabel.setText("Nom du film :");

		filmText = new Text(compositeFilm, SWT.NONE);
		filmText.setLayoutData(centerData);

		genreLabel = new Label(compositeFilm, SWT.NONE);
		genreLabel.setText("Genre :");

		comboGenre = new Combo(compositeFilm, SWT.READ_ONLY);
		comboGenre.setItems(StaticTabs.GENRES);
		comboGenre.select(0);

		yearLabel = new Label(compositeFilm, SWT.NONE);
		yearLabel.setText("Année : ");

		comboYear = new Combo(compositeFilm, SWT.READ_ONLY);
		comboYear.setItems(StaticTabs.YEARS);
		comboYear.select(0);

		noteLabel = new Label(compositeFilm, SWT.NONE);
		noteLabel.setText("Note : ");

		comboNote = new Combo(compositeFilm, SWT.READ_ONLY);
		comboNote.setItems(StaticTabs.NOTES);
		comboNote.select(0);

		synopsisLabel = new Label(compositeFilm, SWT.NONE);
		synopsisLabel.setText("Synopsis:");
		synopsisArea = new Text(compositeFilm, SWT.MULTI | SWT.WRAP
				| SWT.V_SCROLL);
		synopsisArea.setLayoutData(synopsisData);

	}

	public void initFields(String item, MainFrame main) {
		setMainFrame(main);
		ConnexionMysql connect = (ConnexionMysql) main.getFactory();
		ResultSet film = mysqlConnector.requeteSelect("Call ChargerFilm('"
				+ item.replace("'", "''") + "');", (Connection) connect
				.getConnection());

		try {
			film.first();
			filmText.setText(film.getString("nameFilm").trim());
			synopsisArea.setText(film.getString("Synopsis").trim());
			comboNote.select(film.getInt("note") - 1);
			comboYear.select(film.getInt("year"));
			comboGenre.select(film.getInt("genre"));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public MainFrame getMain() {
		return main;
	}

	public void resetFields() {
		filmText.setText("");
		synopsisArea.setText("");
		comboNote.select(0);

	}

	public Button getNext() {
		return suivant;
	}

	public Button getPrev() {
		return precedent;
	}

	public Button getSave() {
		return enregistrer;
	}

	public String getFilm() {
		return filmText.getText();
	}

	/**
	 * @return the synopsisArea
	 */
	public Text getSynopsisArea() {
		return synopsisArea;
	}

	/**
	 * @return the comboGenre
	 */
	public Combo getComboGenre() {
		return comboGenre;
	}

	/**
	 * @return the comboYear
	 */
	public Combo getComboYear() {
		return comboYear;
	}

	/**
	 * @return the comboNote
	 */
	public Combo getComboNote() {
		return comboNote;
	}

	public void setMainFrame(MainFrame main) {
		this.main=main;	
	}

}
